﻿
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class CharacterFactory
{
    public static CharacterFactory Instance {
        get { 
            if(m_Instance == null)
            {
                m_Instance = new CharacterFactory();
                m_Instance.Init();
            }
            return m_Instance;
        }
    } 
    public static CharacterFactory m_Instance =null;

    List<CharacterObjBase> m_Characterbjs = new List<CharacterObjBase>();
    public void Register<T>() where T : new()
    {
        m_Characterbjs.Add(new CharacterObj<T>());
    }
    void Init()
    {
        Register<Bold>();
        Register<Timid>();
        Register<Brave>();
        Register<NiuBi>();
        Register<SD>();
        Register<Obscene>();
        Register<Calm>();
        Register<Change>();
        Register<Crazy>();
    }

    public void Miao()
    {

    }
    public Character CreateCharacter(string name)
    {
        for (int i = 0; i < m_Characterbjs.Count; ++i)
        {
            if (m_Characterbjs[i].m_Name == name)
            {
                return m_Characterbjs[i].CreateCharacter();
            }
        }
        return null;
    }

    public Character RandomCreateOne()
    {
        return m_Characterbjs[Random.Range(0, m_Characterbjs.Count)].CreateCharacter();
    }
}

public class CharacterObjBase
{

    public Character m_Character;
    public string m_Name;
    virtual public Character CreateCharacter()
    {
        return null;
    }
}
public class CharacterObj<T>: CharacterObjBase where T : new()
{
    public CharacterObj()
    {
        m_Character = new T() as Character;
        m_Name = m_Character.m_Kind.ToString();
    }
    public override Character CreateCharacter()
    {
        return new T() as Character;
    }

}